package com.baseproject;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.view.KeyEvent;
import android.view.View;

import com.baseproject.mvp.MvpFragment;
import com.baseproject.util.LogUtils;

import java.util.List;


public abstract class BaseFragmentsActivity extends BaseActivity {

    //布局中Fragment的ID
    protected abstract int getFragmentContentId();

    String topFragmentName;
    FragmentManager mFragmentManager;
    protected MvpFragment defaultFragment;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mFragmentManager = getSupportFragmentManager();
        mFragmentManager.addOnBackStackChangedListener(new FragmentManager.OnBackStackChangedListener() {
            @Override
            public void onBackStackChanged() {
                int backStackCount = mFragmentManager.getBackStackEntryCount();
                if (backStackCount >= 1) {
                    topFragmentName = mFragmentManager.getBackStackEntryAt(backStackCount - 1).getName();
                    LogUtils.d("topFragmentName = " + topFragmentName + " backStackCount = " + backStackCount);
                    MvpFragment topFragment = (MvpFragment) mFragmentManager.findFragmentByTag(topFragmentName);
                    if (null != topFragment) {
                        topFragment.initStatusBar();
                        BaseFragmentsActivity.this.onBackStackChanged(topFragment);
                    }
                }
            }
        });
        setDefaultFragment();
        addFragment(defaultFragment);
    }

    protected abstract void onBackStackChanged(BaseFragment baseFragment);

    protected abstract void setDefaultFragment();

    //添加fragment
    protected void addFragment(final MvpFragment fragment) {
        LogUtils.d("fragment = " + fragment);
        final String tag = fragment.getClass().getSimpleName();
        FragmentManager fragmentManager = getSupportFragmentManager();
        Fragment savedFragment = fragmentManager.findFragmentByTag(tag);
        if (savedFragment == null && fragment != null) {
            FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
            fragmentTransaction.add(getFragmentContentId(), fragment, tag)
                    .addToBackStack(tag)
                    .commit();
        } else if (savedFragment != null) {
            fragmentManager.popBackStack(tag, 0);
        }
    }

    //移除fragment
    protected void removeFragment() {
        if (getSupportFragmentManager().getBackStackEntryCount() > 1) {
            getSupportFragmentManager().popBackStack();
        } else {
            finish();
        }
    }

    protected Fragment getVisibleFragment() {
        List<Fragment> fragments = mFragmentManager.getFragments();
        if (null != fragments) {
            for (Fragment fragment : fragments) {
                if (fragment != null && fragment.isVisible())
                    return fragment;
            }
        }
        return null;
    }

    protected void hideMoreBottomFragment(int count) {
        for (int i = 0; i < count; i++) {
            String tag = mFragmentManager.getBackStackEntryAt(i).getName();
            Fragment fragment = mFragmentManager.findFragmentByTag(tag);
            if (i != count - 1) {
                fragment.getView().setVisibility(View.INVISIBLE);
            } else if (i == count - 1 && fragment.getView().getVisibility() == View.INVISIBLE) {
                fragment.getView().setVisibility(View.VISIBLE);
            }
        }
    }

    public String getTopFragmentName() {
        FragmentManager fragmentManager = getSupportFragmentManager();
        int count = fragmentManager.getBackStackEntryCount();
        if (count >= 1) {
            return fragmentManager.getBackStackEntryAt(count - 1).getName();
        } else {
            return null;
        }
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            if (mFragmentManager.getBackStackEntryCount() > 1) {
                mFragmentManager.popBackStack(null, 0);
                return true;
            } else {
                return super.onKeyDown(keyCode, event);
            }
        }
        return false;
    }
}
